package com.neusoft.nep.admin.controller;

import com.neusoft.nep.admin.entity.Feedback;
import com.neusoft.nep.common.FileUtils;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Modality;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class StatisticsListViewController {
    @FXML
    private TableView<Feedback> tableView;
    @FXML
    private TableColumn<Feedback,String> id;
    @FXML
    private TableColumn<Feedback,String> name;
    @FXML
    private TableColumn<Feedback,String> provence;
    @FXML
    private TableColumn<Feedback,String> city;
    @FXML
    private TableColumn<Feedback,String> AQI;
    @FXML
    private TableColumn<Feedback,String> date;
    @FXML
    private TableColumn<Feedback,Void> colDetail;
    @FXML
    private TableColumn<Feedback,Void> colAssign;
    @FXML
    private ObservableList<Feedback> tableList = FXCollections.observableArrayList();
    @FXML
    private Map feedbackMap=new HashMap();
    @FXML
    private DatePicker datePicker;
    @FXML
    public void initialize(){
        id.setCellValueFactory(new PropertyValueFactory<>("id"));
        name.setCellValueFactory(new PropertyValueFactory<>("name"));
        provence.setCellValueFactory(new PropertyValueFactory<>("provence"));
        city.setCellValueFactory(new PropertyValueFactory<>("city"));
        AQI.setCellValueFactory(new PropertyValueFactory<>("AQI"));
        date.setCellValueFactory(new PropertyValueFactory<>("date"));
            feedbackMap = FileUtils.readMapObject("feedback.txt");
            tableList.addAll(feedbackMap.values());
            tableView.setItems(tableList);
        //动态添加按钮列
        colDetail.setCellFactory(cols->new TableCell<>(){
            private Button btn = new Button("详情");
            {
                btn.setOnAction(e -> {
                    try {
                        Feedback feedback = getTableView().getItems().get(getIndex());
                        // 加载FXML文件
                        FXMLLoader fxmlLoader = new FXMLLoader();
                        URL url = getClass().getResource("../view/AssignFeedbackView.fxml");
                        fxmlLoader.setLocation(url);
                        // 加载视图
                        Parent root = fxmlLoader.load();
                        // 创建对话框
                        Stage dialog = new Stage();
                        dialog.setTitle("公众监督数据列表/公众监督数据详情");
                        dialog.initModality(Modality.APPLICATION_MODAL);
                        dialog.initOwner(((Node) e.getSource()).getScene().getWindow());
                        // 设置场景并显示
                        Scene scene = new Scene(root);
                        dialog.setScene(scene);
                        // 获取控制器并注入dialog引用（如果需要）
                        AssignFeedbackViewController controller = fxmlLoader.getController();
                        if (controller != null) {
                            controller.setDialog(dialog);
                            controller.setData(feedback);
                        }
                        // 显示对话框并等待关闭
                        dialog.showAndWait();
                    } catch (IOException ex) {
                        System.err.println("加载FXML文件时出错: " + ex.getMessage());
                        ex.printStackTrace();
                    }
                });
            }

            @Override
            protected void updateItem(Void item, boolean empty) {
                super.updateItem(item, empty);
                setGraphic(empty?null:btn);
            }
        });
        //动态添加按钮列
        colAssign.setCellFactory(cols->new TableCell<>(){
            private Button btn = new Button("指派");
            {
                btn.setOnAction(e->{
                   try{ // 加载FXML文件
                       Feedback feedback = getTableView().getItems().get(getIndex());
                    FXMLLoader fxmlLoader = new FXMLLoader();
                    URL url = getClass().getResource("../view/AppointView.fxml");
                    fxmlLoader.setLocation(url);
                    // 加载视图
                    Parent root = fxmlLoader.load();
                    // 创建对话框
                    Stage dialog = new Stage();
                    dialog.setTitle("公众监督数据列表/公众监督数据详情");
                    dialog.initModality(Modality.APPLICATION_MODAL);
                    dialog.initOwner(((Node) e.getSource()).getScene().getWindow());
                    // 设置场景并显示
                    Scene scene = new Scene(root);
                    dialog.setScene(scene);
                    // 获取控制器并注入dialog引用（如果需要）
                       AppointViewController controller = fxmlLoader.getController();
                    if (controller != null) {
                        controller.setDialog(dialog);
                        controller.setData(feedback);
                    }
                    // 显示对话框并等待关闭
                    dialog.showAndWait();
                } catch (IOException ex) {
                       System.err.println("加载FXML文件时出错: " + ex.getMessage());
                       ex.printStackTrace();
                   }
                });
            }

            @Override
            protected void updateItem(Void item, boolean empty) {
                super.updateItem(item, empty);
                setGraphic(empty?null:btn);
            }
        });
}}
